package cn.ljq.controller.stu;

import cn.ljq.dao.ClzMapper;
import cn.ljq.dao.LargeFileMapper;
import cn.ljq.dao.MarkMapper;
import cn.ljq.dao.UserMapper;
import cn.ljq.entity.Clz;
import cn.ljq.entity.Mark;
import cn.ljq.entity.Task;
import cn.ljq.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/stu")
public class MyStuController {


    @Autowired
    UserMapper userMapper;

    @Autowired
    LargeFileMapper largeFileMapper;

    @Autowired
    ClzMapper clzMapper;

    @Autowired
    MarkMapper markMapper;


    @GetMapping("/go2updatePwd")
    public String go2updatePwd(Model model, HttpSession session){

        Object who = session.getAttribute("DL");  //拿到 session

        User user = (User) who;  //强转成 User 类

        System.out.println("session == " + user);

        return "/stu/update";
    }

    @PostMapping("/updatePwd")
    public String updatePwd(Model model,String pwd,HttpSession session){

        System.out.println("从前端拿到的修改密码 sb == " + pwd);

        Object who = session.getAttribute("DL");  //拿到 session

        User user = (User)who;  //强转成 User 类

        System.out.println("session  == " + user);

        String myPhome = user.getPhone();  //从session拿到账号

        User newUser = new User();  //定义空对象

        newUser.setPhone(myPhome);  //把账号存入空对象

        newUser.setPwd(pwd);  //把密码存入空对象

        userMapper.updatePwd(newUser);  //SQL修改密码

        return "forward:/public/logout";

    }

    @GetMapping("/findMyCourse")
    public String findMyCourse(Model model,HttpSession session){

        User user = (User) session.getAttribute("DL");

        String clzno = user.getClz().getClzno();  //我的班级编号

        List<Task> myCourses = userMapper.findMyCourse(clzno);  //根据我的班级编号找到我的所有课程

        System.out.println("我的所有课程 == " + myCourses);

        model.addAttribute("myCourses",myCourses);

        return "/stu/myCourse";
    }


    @GetMapping("/findMyMark")
    public String findMyMark(Model model,HttpSession session){

        System.out.println("查询我的成绩");

        User myUser = (User)session.getAttribute("DL");  //从session 拿到我的登录的信息

        System.out.println("session的数据==" + myUser);

        String myPhone = myUser.getPhone();  //拿到我的电话号码

        List<Mark> myMarks = markMapper.findMyMark(myPhone);  //根据电话找到我的所有成绩

        System.out.println("我的所有成绩 == " + myMarks);

        model.addAttribute("myUser",myUser);

        model.addAttribute("myMarks",myMarks);

        return "/stu/myMark";
    }

}
